<?php

/**
 * JingYao-backend
 *
 * @link     https://gitee.com/wang-zhihui-release/jingyao-backend
 * @apiDocument https://gitee.com/wang-zhihui-release/jingyao-backend/wikis/
 */

namespace App\Services;

use App\Enums\AdminActionTypeEnum;
use App\Enums\DevelopingPlaceTypeEnum;
use App\Enums\DevelopingTimeTypeEnum;
use App\Exceptions\ApiException;
use App\Format\DevelopingFormat;
use App\Models\Developing;

class DevelopingService
{
    public function getlists()
    {
        $list = Developing::query()->get();
        $result = [
            DevelopingTimeTypeEnum::desc(DevelopingTimeTypeEnum::HISTORY) => [],
            DevelopingTimeTypeEnum::desc(DevelopingTimeTypeEnum::NOW) => [],
            DevelopingTimeTypeEnum::desc(DevelopingTimeTypeEnum::FUTURE) => [],
        ];
        /** @var Developing $value */
        foreach ($list as $value) {
            array_push($result[DevelopingTimeTypeEnum::desc($value->time_type)], [
                'place' => DevelopingPlaceTypeEnum::desc($value->place_type),
                'content' => $value->content,
            ]);
        }
        return $result;
    }

    public function update(DevelopingFormat $developingFormat)
    {
        $id = $developingFormat->getId();

        $delve = Developing::query()->find($id);

        if (empty($delve)) {
            throw new ApiException('内容不存在', 1);
        }
        $delve->update($developingFormat->toArrayNotNull());
        adminLog(new Developing(), AdminActionTypeEnum::UPDATE, '修改发展历程');
        return true;
    }

    public function getAdminList()
    {
        return Developing::query()->get();
    }
}
